﻿using DX_Recognize.AuxiliaryTool;
using DX_Recognize.Common;
using DX_Recognize.Disposition;
using DX_Recognize.Entitys;
using DX_Recognize.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DX_Recognize.Utils.ConvertUtils;

namespace DX_Recognize
{
    public partial class RecordForm : Form
    {
        public event Action<AMTypeUPloadRecordInfo> ATypeResetRecordOper;
        public event Action<AMTypeUPloadRecordInfo> MTypeResetRecordOper;
        public event Action RenderUIByCondi;

        //搜索条件
        //系统
        public string recordSystemType = "";
        //花式
        public string fancy = null;
        //客户
        public string recordCustomerValue = null;
        //上传人
        public string quoUserCode = null;

        public RecordForm()
        {
            InitializeComponent();
            InitQuoUserrList();
        }

        public void NormalShow()
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            this.Show();
        }

        private void InitQuoUserrList()
        {
            if (UpdateQuoUserBox.Items.Count > 0)
                UpdateQuoUserBox.Items.Clear();
            UpdateQuoUserBox.Items.AddRange(ParamsConfigData.QuoUserList.ToArray());
            UpdateQuoUserBox.DisplayMember = "name";
            UpdateQuoUserBox.ValueMember = "code";
        }

        private void RecordDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string ViewOperName = RecordDataGridView.Columns[e.ColumnIndex].Name;
            //点击查看
            if (ViewOperName.Equals("RecordShowOper"))
            {
               var utypeValue = RecordDataGridView.Rows[e.RowIndex].Cells["uploadType"].Value;
                if(utypeValue != null)
                {
                    string utypeStr = utypeValue.ToString();
                    if (utypeStr.Equals("自动"))
                    {
                       var RecordUIDValue = RecordDataGridView.Rows[e.RowIndex].Cells["RecordUID"].Value;
                       if(RecordUIDValue != null)
                       {
                            String UID = RecordUIDValue.ToString();
                            int index = ParamsConfigData.userAMUploadRIList.FindIndex(item => item.UID.Equals(UID));
                            if(index != -1)
                            {
                                AMTypeUPloadRecordInfo info = ParamsConfigData.userAMUploadRIList[index];
                                ATypeResetRecordOper?.Invoke(info);
                                CADAuxiliaryUtils.OpenDwgByFilePath(info);
                            }
                       }
                    }
                    if (utypeStr.Equals("手动"))
                    {
                        var RecordUIDValue = RecordDataGridView.Rows[e.RowIndex].Cells["RecordUID"].Value;
                        if (RecordUIDValue != null)
                        {
                            String UID = RecordUIDValue.ToString();
                            int index = ParamsConfigData.userAMUploadRIList.FindIndex(item => item.UID.Equals(UID));
                            if (index != -1)
                            {
                                AMTypeUPloadRecordInfo info = ParamsConfigData.userAMUploadRIList[index];
                                MTypeResetRecordOper?.Invoke(info);
                            }
                        }
                    }
                }
            }
        }

        private void RecordForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();//窗口隐藏
            e.Cancel = true;//取消关闭
        }

        private void CustomerBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            VCustomer beChoosedItem = (VCustomer)CustomerSelect.SelectedItem;
            if (beChoosedItem != null)
            {
                string CNum = beChoosedItem.customerNumber;
                CustomerSelect.SelectedValue = CNum;
                recordCustomerValue = CNum;
            }
        }

        //记录 系统类型修改
        private void MoldTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            string itemValue = (string)MoldTypeSelect.SelectedItem;
            if (!recordSystemType.Equals(itemValue))
            {
                List<StandardMoldType> MoldTypeList = ParamsConfigData.MoldTypeList;
                string shortName = "";
                if (itemValue.Equals("大水口"))
                    shortName = "SG";
                if (itemValue.Equals("简化型"))
                    shortName = "TP";
                if (itemValue.Equals("细水口"))
                    shortName = "PP";
                List<string> categoryList = MoldTypeList.FindAll(item => item.shortName.Equals(shortName)).Select(item => item.category).ToList();
                if(FancySelect.Items.Count > 0)
                    FancySelect.Items.Clear();
                for (int i = 0; i < categoryList.Count; i++)
                    FancySelect.Items.Add(categoryList[i]);
                recordSystemType = itemValue;
            }
        }

        private void FancySelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            fancy = (string)FancySelect.SelectedItem;
        }

        //查询按钮点击事件
        private void RecordSearchBtn_Click(object sender, EventArgs e)
        {
            AMTypeUPloadRecordInfo info = new AMTypeUPloadRecordInfo();
            //查询条件注入SQL
            string modeNum = ModeNumText.Texts.ToString();
            string batchNum = BatchNumText.Texts.ToString();
            if(modeNum != "")
                info.modeNum = modeNum;
            if (batchNum != "")
                info.batchNum = batchNum;
            if (fancy != "")
                info.fancy = fancy;
            quoUserCode = (string)RecognizeUtils.dataForm.ULForm.UserAccountText.Texts;
            if (quoUserCode != "")
                info.jobNumber = quoUserCode;
            info.customer = recordCustomerValue;
            Mould.SearchRecordByCondi(info);
            RenderUIByCondi?.Invoke();
        }

        //上传记录查询条件 重置
        private void RecordCondiReSetBtn_Click(object sender, EventArgs e)
        {
            ModeNumText.Texts = null;
            BatchNumText.Texts = null;
        }

        //客户搜索
        private void CustomerBox_TextUpdate(object sender, EventArgs e)
        {
            string CustomerSearchText = CustomerSelect.Text;
            List<VCustomer> OriginList = ParamsConfigData.CustomerList;
            List<VCustomer> TargetList = new List<VCustomer>();
            for (int i = 0; i < OriginList.Count; i++)
            {
                Boolean F = OriginList[i].showContent.Contains(CustomerSearchText);
                if (F)
                    TargetList.Add(OriginList[i]);
            }
            if (CustomerSelect.Items.Count > 0)
                CustomerSelect.Items.Clear();
            CustomerSelect.Items.AddRange(TargetList.ToArray());
            CustomerSelect.DisplayMember = "showContent";
            CustomerSelect.ValueMember = "customerNumber";
            CustomerSelect.DroppedDown = true;
            Cursor = Cursors.Default;
            CustomerSelect.Text = CustomerSearchText;
            CustomerSelect.SelectedValue = null;
            CustomerSelect.Select(CustomerSelect.Text.Length, 0);
        }

        //选择接单上传人
        private void UpdateQuoUserBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            QuoUser beChoosedItem = (QuoUser)UpdateQuoUserBox.SelectedItem;
            if (beChoosedItem != null)
            {
                string code = beChoosedItem.code;
                code = code.Trim();
                UpdateQuoUserBox.SelectedValue = code;
                quoUserCode = code;
            }
        }

    }
}
